import {
  VuexModule,
  Module,
  Action,
  Mutation,
  getModule,
} from 'vuex-module-decorators';
import { setToken, removeToken } from '@/utils/session_storage';
import router, { resetRouter } from '@/router';
import { PermissionModule } from './permission';
import { TagsViewModule } from './tags_view';
import store from '@/store';
import { MyUserState } from '@/interface/my-user-state';

@Module({ dynamic: true, store, name: 'user' })
class User extends VuexModule implements MyUserState {
  public id = '123';
  public nameEn = '';
  public resourceKeys: string[] = [];

  @Action
  public async Login(userInfo: { username: string; password: string }) {
    const { username, password } = userInfo;
    const { data } = {
      data: {
        user: {
          id: '0',
          username: 'admin',
          password: 'any',
          nameEn: 'Super Admin',
          avatar:
            'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          introduction: 'I am a super administrator',
          email: 'admin@test.com',
          phone: '1234567890',
          resourceKeys: ['admin'],
          accessToken: username + '-token',
        }
      }
    };

    const { resourceKeys, nameEn, id } = data.user;
    this.SET_ROLES(resourceKeys);
    this.SET_NAME(nameEn);
    this.SET_ID(id);
    setToken('token-' + id);
  }

  @Action
  public async GetUserInfo() {
    const { data } = await {
      data: {
        user: {
          id: '0',
          username: 'admin',
          password: 'any',
          nameEn: 'Super Admin',
          avatar:
            'https://wpimg.wallstcn.com/f778738c-e4f8-4870-b634-56703b4acafe.gif',
          introduction: 'I am a super administrator',
          email: 'admin@test.com',
          phone: '1234567890',
          resourceKeys: ['admin'],
        },
      },
    };
    if (!data) {
      throw Error('Verification failed, please Login again.');
    }
    const { resourceKeys, nameEn, id } = data.user;
    // roles must be a non-empty array
    if (!resourceKeys || resourceKeys.length <= 0) {
      throw Error('GetUserInfo: roles must be a non-null array!');
    }
    this.SET_ROLES(resourceKeys);
    this.SET_NAME(nameEn);
    this.SET_ID(id);
    setToken('token-' + id);
  }

  @Action
  public async ChangeRoles(role: string) {
    // await this.GetUseInfo();
    resetRouter();
    // Generate dynamic accessible routes based on roles
    PermissionModule.GenerateRoutes(this.resourceKeys);
    // Add generated routes
    router.addRoutes(PermissionModule.dynamicRoutes);
    // Reset visited views and cached views
    TagsViewModule.delAllViews();
  }

  @Action
  public async LogOut() {
    if (this.id === '') {
      throw Error('LogOut: token is undefined!');
    }
    // await logout()
    resetRouter();

    // Reset visited views and cached views
    TagsViewModule.delAllViews();
    this.SET_ROLES([]);
    removeToken();
  }

  @Action
  public ResetToken(id: string) {
    TagsViewModule.delAllViews();
    setToken('token' + id);
    this.SET_ROLES([]);
  }

  @Mutation
  private SET_ID(id: string) {
    this.id = id;
  }

  @Mutation
  private SET_NAME(name: string) {
    this.nameEn = name;
  }

  @Mutation
  private SET_ROLES(roles: string[]) {
    this.resourceKeys = roles;
  }
}

export const UserModule = getModule(User);
